package com.ruoyi.project.stop.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.stop.domain.ShipRecord;
import com.ruoyi.project.stop.domain.WarnHistoryVO;
import com.ruoyi.project.stop.dto.WarnHistoryDTO;
import com.ruoyi.project.stop.mapper.ShipRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stop.mapper.WarnHistoryMapper;
import com.ruoyi.project.stop.domain.WarnHistory;
import com.ruoyi.project.stop.service.IWarnHistoryService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 报警历史记录单据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class WarnHistoryServiceImpl implements IWarnHistoryService
{
    @Autowired
    private WarnHistoryMapper warnHistoryMapper;

    @Autowired
    private ShipRecordMapper shipRecordMapper;

    /**
     * 查询报警历史记录单据
     *
     * @param id 报警历史记录单据主键
     * @return 报警历史记录单据
     */
    @Override
    public WarnHistory selectWarnHistoryById(Long id)
    {
        return warnHistoryMapper.selectWarnHistoryById(id);
    }

    /**
     * 查询报警历史记录单据列表
     *
     * @param warnHistory 报警历史记录单据
     * @return 报警历史记录单据
     */
    @Override
    public List<WarnHistory> selectWarnHistoryList(WarnHistory warnHistory)
    {

        return warnHistoryMapper.selectWarnHistoryList(warnHistory);
    }
    @Override
    public List<WarnHistoryDTO> selectWarnHistoryDTOList(WarnHistoryVO warnHistoryVO)
    {
        List<WarnHistory> warnHistoryList = warnHistoryMapper.selectWarnHistoryListNew(warnHistoryVO);
        List<WarnHistoryDTO> warnHistoryDTOList = new ArrayList<>();
        for (WarnHistory warn:warnHistoryList) {
            WarnHistoryDTO warnHistoryDTO = new WarnHistoryDTO();
            BeanUtils.copyProperties(warn, warnHistoryDTO);
            if(warn.getShipId() !=null){
                ShipRecord shipRecord =  shipRecordMapper.selectShipRecordById(warn.getShipId());
                if(shipRecord !=null){
                    warnHistoryDTO.setShipName(shipRecord.getShipName());
                }
            }
            warnHistoryDTOList.add(warnHistoryDTO);
        }
        return warnHistoryDTOList;
    }
    /**
     * 新增报警历史记录单据
     *
     * @param warnHistory 报警历史记录单据
     * @return 结果
     */
    @Override
    public int insertWarnHistory(WarnHistory warnHistory)
    {
        warnHistory.setCreateTime(DateUtils.getNowDate());
        return warnHistoryMapper.insertWarnHistory(warnHistory);
    }

    /**
     * 修改报警历史记录单据
     *
     * @param warnHistory 报警历史记录单据
     * @return 结果
     */
    @Override
    public int updateWarnHistory(WarnHistory warnHistory)
    {
        warnHistory.setUpdateTime(DateUtils.getNowDate());
        return warnHistoryMapper.updateWarnHistory(warnHistory);
    }

    /**
     * 批量删除报警历史记录单据
     *
     * @param ids 需要删除的报警历史记录单据主键
     * @return 结果
     */
    @Override
    public int deleteWarnHistoryByIds(String ids)
    {
        return warnHistoryMapper.deleteWarnHistoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除报警历史记录单据信息
     *
     * @param id 报警历史记录单据主键
     * @return 结果
     */
    @Override
    public int deleteWarnHistoryById(Long id)
    {
        return warnHistoryMapper.deleteWarnHistoryById(id);
    }
}
